<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .box {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
            margin: auto;
        }

        .box>.head {
            display: flex;
            justify-content: space-between;
        }

        .box>.head>li {
            flex-grow: 1;
            text-align: center;
            cursor: pointer;
        }

        .active {
            background-color: #87ceeb;
        }

        .showBox {
            position: relative;
            width: 300px;
            height: 179px;
        }

        .showBox div {
            width: 300px;
            height: 179px;
            position: absolute;
            display: none;            
        }

        .main{
            display: block;
        }

        span{
            font-size: larger;
            position: absolute;
            top: 40%;
            left: 45%;
        }

        #mBox1{
            background-color: red;
        }

        #mBox2{
            background-color: green;
        }

        #mBox3{
            background-color: skyblue;
        }

        .active1{
            display: block !important;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li class="active">晴天</li>
            <li>阴天</li>
            <li>雨天</li>
        </ul>

        <div class="showBox">
            <div class="main" id="mBox1">
                <span>晴天</span>
            </div>
            <div class="main" id="mBox2">
                <span>阴天</span>
            </div>
            <div class="main" id="mBox3">
                <span>雨天</span>
            </div>
        </div>

    </div>

    <script>
        let list = document.querySelectorAll(".head > li");
        let main = document.querySelector(".main");
        let showBox = document.querySelectorAll(".showBox > div");
        console.log(showBox);

        let lastIndex = 0; // 设置上一步操作的下标

        //  head容器每个li绑定事件
        list.forEach(function (item, index) {
            item.onclick = function () {
                // li里面的active类名全部移除
                // list.forEach(function (item) {
                //     item.classList.remove("active");
                // });

                // 清除上一个操作的类名
                list[lastIndex].classList.remove("active");
                // 当前li操作添加类名
                item.classList.add("active");
                // 保存操作完毕之后的下标
                // 点击更换内容
                showBox[lastIndex].classList.remove("active1");
                showBox[index].classList.add("active1");
                lastIndex = index;

                // showBox.innerText = item.innerText;
                // 作业(不同的标题对应不同的容器)
            }
        });
    </script>
</body>
</html>